import env from '@/common/api/env.js';
import {getToken,setToken} from '@/common/api/auth.js';

export default{
	// getEventRunTime(type){
	// 	let eventTime = 3000;
	// 	if(type === 'wm-clock'){
	// 		eventTime = 1000;
	// 	}
	// 	return eventTime;
	// },
	
	// getLineHeight(fontSize){
		
	// },
	
	drawRoundRect(ctx, x, y, w, h , r, fillColor){
		if (w < 2 * r) {r = w / 2;}
		if (h < 2 * r){ r = h / 2;}
		ctx.beginPath();
		ctx.moveTo(x+r, y);
		ctx.arcTo(x+w, y, x+w, y+h, r);
		ctx.arcTo(x+w, y+h, x, y+h, r);
		ctx.arcTo(x, y+h, x, y, r);
		ctx.arcTo(x, y, x+w, y, r);
		ctx.closePath();
		ctx.fillStyle = fillColor;
		// ctx.setFillStyle(fillColor);
		ctx.fill()
	},
	
	drawRoundRectWithTop(ctx, x, y, w, h , r, fillColor){
		if (w < 2 * r) {r = w / 2;}
		if (h < 2 * r){ r = h / 2;}
		ctx.beginPath();
		ctx.moveTo(x+r, y);

		ctx.arcTo(x+w, y, x+w, y+h, r);
		ctx.lineTo(x + w, y + h);
		ctx.lineTo(x, y + h);
		ctx.arcTo(x, y, x+w, y, r);
		
		ctx.closePath();
		ctx.fillStyle = fillColor;
		
		// ctx.setFillStyle(fillColor);
		ctx.fill()
	},
	
	drawRoundRectWithBottom(ctx, x, y, w, h , r, fillColor){
		if (w < 2 * r) {r = w / 2;}
		if (h < 2 * r){ r = h / 2;}
		ctx.beginPath();
		ctx.moveTo(x, y);
		ctx.lineTo(x + w, y);
		ctx.arcTo(x+w, y+h, x, y+h, r);
		ctx.arcTo(x, y+h, x, y, r);
		ctx.closePath();
		
		ctx.fillStyle = fillColor;
		// ctx.setFillStyle(fillColor);
		ctx.fill()
	},
	
	// 根据单行最大宽度拆分文字为数组
	// ctx画布  rowTextWidth一行文字的最大宽度  location内容  drawX/x坐标  drawY/y坐标  rowHeight行高
	substring2List(ctx, rowTextWidth, location, drawX, drawY, rowHeight, fontStyle){
		
		// if(true){
		// 	return this.substring2ListV2(ctx, rowTextWidth, location, drawX, drawY, rowHeight,fontStyle);
		// }
		
		if(rowTextWidth <= 0 || !location || '' === location) 
			return {drawY: drawY, row: 0, message:'error'};
		
		// 去除两侧的空格
		location = location.replace(/^\s|\s$/g,"");	
		
		// 文本总宽度
		let textWidth = ctx.measureText(location).width;
		
		// 单行可显示内容
		if(textWidth <= rowTextWidth){
			ctx.fillText(location, drawX, drawY);
			return {drawY: drawY + rowHeight * 1.25, row: 1};
		}
		// 多行拆分
		else{
			// 单个字的宽度
			let sigleWordWidth = textWidth / location.length + 2;
			
			// 标题的宽度
			let titleWidth = 0;
			let cutIndex = location.indexOf('：');
			let titleText = location.substring(0, cutIndex+1);
			titleWidth = ctx.measureText(titleText).width;
			
			// 去除标题
			location = location.replace(titleText,'');
			// 单行最大字数
			let sigleRowWord = (rowTextWidth - titleWidth) / sigleWordWidth;
			// 总共多少行
			let allRow = (location.length / sigleRowWord) + ((location.length % sigleRowWord) > 0 ? 1 : 0);
			let list = [];
			for(let i = 0 ; i < allRow ; i++){
				let rowText = location.substring(i*sigleRowWord, (i+1)*sigleRowWord);
				list.push(rowText);
			}
			
			let lastY = 0;
			// X轴偏移量 用于有多行时，第二行从标题右侧开始绘制内容
			let offsetX = 0;
			list.forEach((item,i)=>{
				let y = drawY + i * rowHeight;
				lastY = y;
				
				if(i === 0){
					ctx.fillText(titleText, drawX, lastY);
				}
				ctx.fillText(item, drawX + titleWidth, lastY);
			});
			
			// 增加行高的一半作为间距
			lastY += (rowHeight * 0.25);
			return {drawY: lastY, row: list.length};
		}
	},
	
	// 根据单行最大宽度拆分文字为数组
	// ctx画布  rowTextMaxWidth一行文字的最大宽度  location内容  drawX/x坐标  drawY/y坐标  rowHeight行高
	substring2ListV2(ctx, rowTextMaxWidth, location, drawX, drawY, rowHeight, fontStyle){
		if(rowTextMaxWidth <= 0 || !location || '' === location) 
			return {drawY: drawY, row: 0, message:'error'};
		
		// 去除两侧的空格
		location = location.replace(/^\s|\s$/g,"");	
		
		// 文本总宽度
		if(fontStyle){
			ctx.font = fontStyle;
		}
		
		let textWidth = ctx.measureText(location).width;
		
		// 单行可显示内容
		if(textWidth <= rowTextMaxWidth){
			ctx.fillText(location, drawX, drawY);
			return {drawY: drawY + rowHeight * 1.25, row: 1};
		}
		// 多行拆分
		else{
			// 单个字的宽度
			let sigleWordWidth = textWidth / location.length + 2;
			
			// 计算标题的宽度 START
			let titleWidth = 0;
			let cutIndex = location.indexOf('：');
			let titleText = location.substring(0, cutIndex+1);
			titleWidth = ctx.measureText(titleText).width;
			// 计算标题的宽度 END
			
			// 从文本中先去除标题
			location = location.replace(titleText,'');
			
			let re = /[^\u4E00-\u9FA5]/;
			let chinesWordWidth = ctx.measureText('国').width;
			let otherWordWidth = ctx.measureText('2').width;
			// 当前行的累加宽度
			let textWidthCount = 0;
			let textCount = '';
			
			let textList = [];
			
			for(let i = 0 ; i < location.length ; i++){
				let word = location[i];
				let wordWidth = re.test(word) ? chinesWordWidth : otherWordWidth;
				
				textWidthCount += wordWidth;
				textCount += word;
				
				if(textWidthCount >= rowTextMaxWidth){
					console.error("行文字：",textWidthCount, rowTextMaxWidth);
					textList.push(textCount);
					textCount = '';
					textWidthCount = 0;
				}else if(i >= location.length - 1){
					textList.push(textCount);
					textCount = '';
					textWidthCount = 0;
				}
				
				// console.error("字符：",word, '是否汉字1：',re.test(word));
			}
			console.error("文字列表：",textList);
			
			    // var re=/[^\u4E00-\u9FA5]/;
			    // if (re.test(temp)) return false ;
			    // return true ;
			
			
			// // 单行最大字数
			// let sigleRowWord = (rowTextMaxWidth - titleWidth) / sigleWordWidth;
			// // 总共多少行
			// let allRow = (location.length / sigleRowWord) + ((location.length % sigleRowWord) > 0 ? 1 : 0);
			// let list = [];
			// for(let i = 0 ; i < allRow ; i++){
			// 	let rowText = location.substring(i*sigleRowWord, (i+1)*sigleRowWord);
			// 	list.push(rowText);
			// }
			
			// console.error("原列表：",list);
			
			let lastY = 0;
			// X轴偏移量 用于有多行时，第二行从标题右侧开始绘制内容
			let offsetX = 0;
			textList.forEach((item,i)=>{
				let y = drawY + i * rowHeight;
				lastY = y;
				
				if(i === 0){
					ctx.fillText(titleText, drawX, lastY);
				}
				ctx.fillText(item, drawX + titleWidth, lastY);
			});
			
			// 增加行高的一半作为间距
			lastY += (rowHeight);
			return {drawY: lastY, row: textList.length};
		}
	},
	
	// 模版组件查询是否显示编辑状态
	flagIsShowEditView(waterMarkName){
		return uni.getStorageSync('current_water_mark_name') === waterMarkName;
	},
	
	// 清除当前使用的水印模版名字
	clearShowEditFlag(){
		uni.setStorageSync('current_water_mark_name','');
	},
	
	// 获取水印模板图片
	getWaterMarkImage(that, width, height){
		var b = new Promise(function(resolve, reject){
			uni.canvasToTempFilePath({quality:1,x:0,y:0,width:width,height:height, canvasId:'canvasMark',success:res=>{
				res.width = width;
				res.height = height;
				resolve(res);
			},fail: (err) => {
				reject(err);
			}},that);
		});
		return b;
	},
	
	uploadFile(path){
		const urlPath = `${env.baseUrl}${"/entry/recruitment/uploadResume"}`;
		let tokenValue = getToken();
		// console.error("小程序token:",getToken());
		
		const uploadTask = uni.uploadFile({
				url: urlPath, //仅为示例，非真实的接口地址
				filePath: path,
				name: 'file',
				timeout:300 * 1000,  // 五分钟超时
				header: {
					'Content-Type': 'application/json; charset=UTF-8', // 默认值
					'Source-Type': 'WEB',
					'Accept': '*/*',
					'lanlinker-satoken': tokenValue,
					'Referer': 'https://www.gyhapp.com' // 不支持
				},
				success: (uploadFileRes) => {
					if(uploadFileRes.data.code === 'A0300' || uploadFileRes.data.indexOf('A0300') !== -1){
						setToken('');
						uni.showToast({
							title:'请通过工友慧app使用本功能',
							icon:'none'
						});
						return;
					}
					
					uni.showToast({
						title:'已上传至云相册',
						icon:'none'
					})
					console.log("上传成功：",uploadFileRes);
				},
				fail: (e)=>{
					console.log("上传失败",e);
					
				}
			});
	}
}